package com.cskaoyan.service;

import com.cskaoyan.bean.role.*;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    public static final String systemPermissions = "{ 'list1' : [{\"id\":\"系统管理\",\"label\":\"系统管理\",\"children\":[{\"id\":\"管理员管理\",\"label\":\"管理员管理\",\"children\":[{\"id\":\"admin:admin:update\",\"label\":\"编辑\",\"api\":\"POST /admin/admin/update\"},{\"id\":\"admin:admin:list\",\"label\":\"查询\",\"api\":\"GET /admin/admin/list\"},{\"id\":\"admin:admin:read\",\"label\":\"详情\",\"api\":\"GET /admin/admin/read\"},{\"id\":\"admin:admin:delete\",\"label\":\"删除\",\"api\":\"POST /admin/admin/delete\"},{\"id\":\"admin:admin:create\",\"label\":\"添加\",\"api\":\"POST /admin/admin/create\"}]},{\"id\":\"角色管理\",\"label\":\"角色管理\",\"children\":[{\"id\":\"admin:role:permission:update\",\"label\":\"权限变更\",\"api\":\"POST /admin/role/permissions\"},{\"id\":\"admin:role:update\",\"label\":\"角色编辑\",\"api\":\"POST /admin/role/update\"},{\"id\":\"admin:role:list\",\"label\":\"角色查询\",\"api\":\"GET /admin/role/list\"},{\"id\":\"admin:role:read\",\"label\":\"角色详情\",\"api\":\"GET /admin/role/read\"},{\"id\":\"admin:role:delete\",\"label\":\"角色删除\",\"api\":\"POST /admin/role/delete\"},{\"id\":\"admin:role:permission:get\",\"label\":\"权限详情\",\"api\":\"GET /admin/role/permissions\"},{\"id\":\"admin:role:create\",\"label\":\"角色添加\"," +
            "\"api\":\"POST /admin/role/create\"}]},{\"id\":\"对象存储\",\"label\":\"对象存储\",\"children\":[{\"id\":\"admin:storage:update\",\"label\":\"编辑\",\"api\":\"POST /admin/storage/update\"},{\"id\":\"admin:storage:list\",\"label\":\"查询\",\"api\":\"GET /admin/storage/list\"},{\"id\":\"admin:storage:read\",\"label\":\"详情\",\"api\":\"POST /admin/storage/read\"},{\"id\":\"admin:storage:delete\",\"label\":\"删除\",\"api\":\"POST /admin/storage/delete\"},{\"id\":\"admin:storage:create\",\"label\":\"上传\",\"api\":\"POST /admin/storage/create\"}]},{\"id\":\"操作日志\",\"label\":\"操作日志\",\"children\":[{\"id\":\"admin:log:list\",\"label\":\"查询\",\"api\":\"GET /admin/log/list\"}]}]},{\"id\":\"商场管理\",\"label\":\"商场管理\",\"children\":[{\"id\":\"品牌管理\",\"label\":\"品牌管理\",\"children\":[{\"id\":\"admin:brand:update\",\"label\":\"编辑\",\"api\":\"POST /admin/brand/update\"},{\"id\":\"admin:brand:list\",\"label\":\"查询\",\"api\":\"GET /admin/brand/list\"},{\"id\":\"admin:brand:read\",\"label\":\"详情\",\"api\":\"GET /admin/brand/read\"},{\"id\":\"admin:brand:delete\",\"label\":\"删除\",\"api\":\"POST /admin/brand/delete\"},{\"id\":\"admin:brand:create\",\"label\":\"添加\",\"api\":\"POST /admin/brand/create\"}]},{\"id\":\"通用问题\",\"label\":\"通用问题\"," +
            "\"children\":[{\"id\":\"admin:issue:update\",\"label\":\"编辑\",\"api\":\"POST /admin/issue/update\"},{\"id\":\"admin:issue:list\",\"label\":\"查询\",\"api\":\"GET /admin/issue/list\"},{\"id\":\"admin:issue:delete\",\"label\":\"删除\",\"api\":\"POST /admin/issue/delete\"},{\"id\":\"admin:issue:create\",\"label\":\"添加\",\"api\":\"POST /admin/issue/create\"}]},{\"id\":\"订单管理\",\"label\":\"订单管理\",\"children\":[{\"id\":\"admin:order:read\",\"label\":\"详情\",\"api\":\"GET /admin/order/detail\"},{\"id\":\"admin:order:refund\",\"label\":\"订单退款\",\"api\":\"POST /admin/order/refund\"},{\"id\":\"admin:order:reply\",\"label\":\"订单商品回复\",\"api\":\"POST /admin/order/reply\"},{\"id\":\"admin:order:ship\",\"label\":\"订单发货\",\"api\":\"POST /admin/order/ship\"},{\"id\":\"admin:order:list\",\"label\":\"查询\",\"api\":\"GET /admin/order/list\"}]},{\"id\":\"类目管理\",\"label\":\"类目管理\",\"children\":[{\"id\":\"admin:category:update\",\"label\":\"编辑\",\"api\":\"POST /admin/category/update\"},{\"id\":\"admin:category:list\",\"label\":\"查询\",\"api\":\"GET /admin/category/list\"},{\"id\":\"admin:category:read\",\"label\":\"详情\",\"api\":\"GET /admin/category/read\"},{\"id\":\"admin:category:delete\",\"label\":\"删除\",\"api\":\"POST /admin/category/delete\"}," +
            "{\"id\":\"admin:category:create\",\"label\":\"添加\",\"api\":\"POST /admin/category/create\"}]},{\"id\":\"关键词\",\"label\":\"关键词\",\"children\":[{\"id\":\"admin:keyword:update\",\"label\":\"编辑\",\"api\":\"POST /admin/keyword/update\"},{\"id\":\"admin:keyword:list\",\"label\":\"查询\",\"api\":\"GET /admin/keyword/list\"},{\"id\":\"admin:keyword:read\",\"label\":\"详情\",\"api\":\"GET /admin/keyword/read\"},{\"id\":\"admin:keyword:delete\",\"label\":\"删除\",\"api\":\"POST /admin/keyword/delete\"},{\"id\":\"admin:keyword:create\",\"label\":\"添加\",\"api\":\"POST /admin/keyword/create\"}]}]},{\"id\":\"用户管理\",\"label\":\"用户管理\",\"children\":[{\"id\":\"用户收藏\",\"label\":\"用户收藏\",\"children\":[{\"id\":\"admin:collect:list\",\"label\":\"查询\",\"api\":\"GET /admin/collect/list\"}]},{\"id\":\"用户足迹\",\"label\":\"用户足迹\",\"children\":[{\"id\":\"admin:footprint:list\",\"label\":\"查询\",\"api\":\"GET /admin/footprint/list\"}]},{\"id\":\"意见反馈\",\"label\":\"意见反馈\",\"children\":[{\"id\":\"admin:feedback:list\",\"label\":\"查询\",\"api\":\"GET /admin/feedback/list\"}]},{\"id\":\"搜索历史\",\"label\":\"搜索历史\",\"children\":[{\"id\":\"admin:history:list\",\"label\":\"查询\",\"api\":\"GET /admin/history/list\"}]},{\"id\":\"会员管理\",\"label\":\"会员管理\"," +
            "\"children\":[{\"id\":\"admin:user:list\",\"label\":\"查询\",\"api\":\"GET /admin/user/list\"}]},{\"id\":\"收货地址\",\"label\":\"收货地址\",\"children\":[{\"id\":\"admin:address:list\",\"label\":\"查询\",\"api\":\"GET /admin/address/list\"}]}]},{\"id\":\"配置管理\",\"label\":\"配置管理\",\"children\":[{\"id\":\"小程序配置\",\"label\":\"小程序配置\",\"children\":[{\"id\":\"admin:config:wx:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/wx\"},{\"id\":\"admin:config:wx:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/wx\"}]},{\"id\":\"运费配置\",\"label\":\"运费配置\",\"children\":[{\"id\":\"admin:config:express:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/express\"},{\"id\":\"admin:config:express:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/express\"}]},{\"id\":\"商场配置\",\"label\":\"商场配置\",\"children\":[{\"id\":\"admin:config:mall:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/mall\"},{\"id\":\"admin:config:mall:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/mall\"}]},{\"id\":\"订单配置\",\"label\":\"订单配置\",\"children\":[{\"id\":\"admin:config:order:list\",\"label\":\"详情\",\"api\":\"GET /admin/config/order\"},{\"id\":\"admin:config:order:updateConfigs\",\"label\":\"编辑\",\"api\":\"POST /admin/config/order\"}]}]}," +
            "{\"id\":\"推广管理\",\"label\":\"推广管理\",\"children\":[{\"id\":\"广告管理\",\"label\":\"广告管理\",\"children\":[{\"id\":\"admin:ad:update\",\"label\":\"编辑\",\"api\":\"POST /admin/ad/update\"},{\"id\":\"admin:ad:list\",\"label\":\"查询\",\"api\":\"GET /admin/ad/list\"},{\"id\":\"admin:ad:read\",\"label\":\"详情\",\"api\":\"GET /admin/ad/read\"},{\"id\":\"admin:ad:delete\",\"label\":\"删除\",\"api\":\"POST /admin/ad/delete\"},{\"id\":\"admin:ad:create\",\"label\":\"添加\",\"api\":\"POST /admin/ad/create\"}]},{\"id\":\"专题管理\",\"label\":\"专题管理\",\"children\":[{\"id\":\"admin:topic:update\",\"label\":\"编辑\",\"api\":\"POST /admin/topic/update\"},{\"id\":\"admin:topic:list\",\"label\":\"查询\",\"api\":\"GET /admin/topic/list\"},{\"id\":\"admin:topic:read\",\"label\":\"详情\",\"api\":\"GET /admin/topic/read\"},{\"id\":\"admin:topic:delete\",\"label\":\"删除\",\"api\":\"POST /admin/topic/delete\"},{\"id\":\"admin:topic:create\",\"label\":\"添加\",\"api\":\"POST /admin/topic/create\"}]},{\"id\":\"优惠券管理\",\"label\":\"优惠券管理\",\"children\":[{\"id\":\"admin:coupon:listuser\",\"label\":\"查询用户\",\"api\":\"GET /admin/coupon/listuser\"},{\"id\":\"admin:coupon:update\",\"label\":\"编辑\",\"api\":\"POST /admin/coupon/update\"},{\"id\":\"admin:coupon:list\",\"label\":\"查询\",\"api\":\"GET /admin/coupon/list\"}," +
            "{\"id\":\"admin:coupon:read\",\"label\":\"详情\",\"api\":\"GET /admin/coupon/read\"},{\"id\":\"admin:coupon:delete\",\"label\":\"删除\",\"api\":\"POST /admin/coupon/delete\"},{\"id\":\"admin:coupon:create\",\"label\":\"添加\",\"api\":\"POST /admin/coupon/create\"}]},{\"id\":\"团购管理\",\"label\":\"团购管理\",\"children\":[{\"id\":\"admin:groupon:read\",\"label\":\"详情\",\"api\":\"GET /admin/groupon/listRecord\"},{\"id\":\"admin:groupon:update\",\"label\":\"编辑\",\"api\":\"POST /admin/groupon/update\"},{\"id\":\"admin:groupon:list\",\"label\":\"查询\",\"api\":\"GET /admin/groupon/list\"},{\"id\":\"admin:groupon:delete\",\"label\":\"删除\",\"api\":\"POST /admin/groupon/delete\"},{\"id\":\"admin:groupon:create\",\"label\":\"添加\",\"api\":\"POST /admin/groupon/create\"}]}]},{\"id\":\"商品管理\",\"label\":\"商品管理\",\"children\":[{\"id\":\"商品管理\",\"label\":\"商品管理\",\"children\":[{\"id\":\"admin:goods:read\",\"label\":\"详情\",\"api\":\"GET /admin/goods/detail\"},{\"id\":\"admin:goods:update\",\"label\":\"编辑\",\"api\":\"POST /admin/goods/update\"},{\"id\":\"admin:goods:list\",\"label\":\"查询\",\"api\":\"GET /admin/goods/list\"},{\"id\":\"admin:goods:delete\",\"label\":\"删除\",\"api\":\"POST /admin/goods/delete\"},{\"id\":\"admin:goods:create\",\"label\":\"上架\",\"api\":\"POST /admin/goods/create\"}]}," +
            "{\"id\":\"评论管理\",\"label\":\"评论管理\",\"children\":[{\"id\":\"admin:comment:list\",\"label\":\"查询\",\"api\":\"GET /admin/comment/list\"},{\"id\":\"admin:comment:delete\",\"label\":\"删除\",\"api\":\"POST /admin/comment/delete\"}]}]},{\"id\":\"其他\",\"label\":\"其他\",\"children\":[{\"id\":\"权限测试\",\"label\":\"权限测试\",\"children\":[{\"id\":\"index:permission:write\",\"label\":\"权限写\",\"api\":\"POST /admin/index/write\"},{\"id\":\"index:permission:read\",\"label\":\"权限读\",\"api\":\"GET /admin/index/read\"}]}]},{\"id\":\"统计管理\",\"label\":\"统计管理\",\"children\":[{\"id\":\"用户统计\",\"label\":\"用户统计\",\"children\":[{\"id\":\"admin:stat:user\",\"label\":\"查询\",\"api\":\"GET /admin/stat/user\"}]},{\"id\":\"订单统计\",\"label\":\"订单统计\",\"children\":[{\"id\":\"admin:stat:order\",\"label\":\"查询\",\"api\":\"GET /admin/stat/order\"}]},{\"id\":\"商品统计\",\"label\":\"商品统计\",\"children\":[{\"id\":\"admin:stat:goods\",\"label\":\"查询\",\"api\":\"GET /admin/stat/goods\"}]}]}]}";


    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;
    @Override
    public List<RoleOptionVO> options() {
        return roleMapper.selectRoleOption();
    }

    @Override
    public RoleListVO list(Integer page, Integer limit, String name, String sort, String order) {
        PageHelper.startPage(page,limit);
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (!StringUtils.isEmpty(name)){
            criteria.andNameLike("%" + name + "%");
        }
        roleExample.setOrderByClause(sort + " " + order);
        List<Role> roles = roleMapper.selectByExample(roleExample);

        PageInfo pageInfo = new PageInfo(roles);
        long total = pageInfo.getTotal();
        RoleListVO roleListVO = new RoleListVO();
        roleListVO.setItems(roles);
        roleListVO.setTotal(total);
        return roleListVO;
    }

    @Override
    @Transactional
    public Role create(Role role) {
        if (HadRoleByName(role.getName())) {
            return new Role();
        }
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insertSelective(role);
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andNameEqualTo(role.getName());
        List<Role> roles = roleMapper.selectByExample(roleExample);
        return roles.get(0);
    }

    @Override
    public int update(Role role) {
        role.setUpdateTime(new Date());
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andNameEqualTo(role.getName());
        List<Role> roles = roleMapper.selectByExample(roleExample);
        if (roles.size() != 0) {
            return 0;
        }
        roleMapper.updateByPrimaryKeySelective(role);
        return 1;
    }


    @Override
    @Transactional
    public void delete(Role role) {
        roleMapper.deleteByPrimaryKey(role.getId());
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        criteria.andRoleIdEqualTo(role.getId());
        permissionMapper.deleteByExample(permissionExample);
    }

//    /**
//     * 将systempermissions存入数据库
//     * @param systemPermissions
//     * @return
//     */
//    @Override
//    @Transactional
//    public void store(SystemPermissions systemPermissions) {
//        List<FirstPermission> firstPermissions = systemPermissions.getSystemPermissions();
//        for (FirstPermission firstPermission : firstPermissions) {
//            roleMapper.insertFirstLevelPermission(firstPermission);
//            for (SecondPermissions child : firstPermission.getChildren()) {
//                roleMapper.insertSecondLevelPermissions(child,firstPermission.getId());
//                for (ThirdPermissions childChild : child.getChildren()) {
//                    roleMapper.insertApi(childChild,child.getId());
//                }
//            }
//        }
//    }

    @Override
    @Transactional
    public PermissionBean permissions(Integer roleId) {
        PermissionBean permissionBean = new PermissionBean();
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        criteria.andRoleIdEqualTo(roleId);
        List<Permission> permissions = permissionMapper.selectByExample(permissionExample);
        List<String> list = new ArrayList<>();
        if (permissions.size() != 0 && permissions.get(0).getPermission().equals("*")) {
            list = permissionMapper.selectAllLastPermission();
        }
        if (permissions.size() != 0) {
            for (Permission permission : permissions) {
                list.add(permission.getPermission());
            }
        }
        permissionBean.setAssignedPermissions(list);

        Gson gson = new Gson();
        SystemPermissionTemp systemPermissionTemp = gson.fromJson(systemPermissions, SystemPermissionTemp.class);
        //List<FirstPermission> list1 = permissionMapper.selectAllPermission();
        List<FirstPermission> list1 = systemPermissionTemp.getList1();
        permissionBean.setSystemPermissions(list1);
        return permissionBean;
    }

    @Override
    @Transactional
    public int permissions2(PermissionBO permissionBO) {
        String s = roleMapper.selectNameById(permissionBO.getRoleId());
        if (s.equals("超级管理员")){
            return 0;
        }
        PermissionExample permissionExample = new PermissionExample();
        PermissionExample.Criteria criteria = permissionExample.createCriteria();
        criteria.andRoleIdEqualTo(permissionBO.getRoleId());
        permissionMapper.deleteByExample(permissionExample);
        if (permissionBO.getPermissions().size() != 0) {
            for (String permission : permissionBO.getPermissions()) {
                Permission permission1 = new Permission();
                permission1.setPermission(permission);
                permission1.setAddTime(new Date());
                permission1.setRoleId(permissionBO.getRoleId());
                permission1.setUpdateTime(new Date());
                permissionMapper.insert(permission1);
            }
        }
        return 1;
    }


    private boolean HadRoleByName(String name) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andNameEqualTo(name);
        long count = roleMapper.countByExample(roleExample);
        return count != 0 ? true:false;
    }

}
